using System;
using System.Collections.Generic;
using System.Text;
using Presentation.Contracts;
using Task.Contracts;
using DataAccess;
using Globals;
using Task;
using System.Data;

namespace Presentation
{
    public class FriendsPresenter
    {
        #region Members

		/// <summary>
		/// Instance of view.
		/// </summary>
		private IFriendsView view;

		/// <summary>
		/// Simple task.
		/// </summary>
		private IFriendsTask task;

		#endregion Members

        #region Construction/Finalization

		/// <summary>
		/// Initializes a new instance of the <see cref="AddForumPresenter"/> class.
		/// </summary>
		/// <param name="view">The view.</param>
		public FriendsPresenter(IFriendsView view)
			{
			this.view = view;
			task = new FriendsTask();
			}

        public FriendsPresenter()
        {
        }

		#endregion Construction/Finalization

		#region Public Methods

		/// <summary>
		/// Initializes view.
		/// </summary>
		public void Initialize()
			{
			
			}


        public void SetCaption()
        {
            view.SetHeader(view.StrID());
        }
		/// <summary>
		/// 
		/// </summary>
		public int FindNrOfPages()
			{
			int M = task.GetNumberOfFriends(int.Parse(view.StrID()));
            int numberOfPages = 0;
            int N=task.FriendsPerPage;
            
             if (M % N != 0)
                numberOfPages = M / N + 1;
             else
                numberOfPages = M / N;
	
            return numberOfPages;			

			}

        public int GetNumberOfAllFriends()
        {
            return task.GetNumberOfFriends(int.Parse(view.StrID()));
        }

        public int GetNumberOfAllSentInvitations()
        {
            return task.GetNumberOfSentInvitations(int.Parse(view.SessionID()));
        }

        public int GetNumberOfAllReceivedInvitations()
        {
            return task.GetNumberOfReceivedInvitations(int.Parse(view.SessionID()));
        }

        public void FindFriendsPerPage(int pageNr){
            DataTable dt= task.GetAllInfoFriends(int.Parse(view.StrID()), pageNr);
            view.ShowFriends(dt,0);
        }

        public bool FindCommonFriends(int UserId,int User2Id,int FrId){
        
            int[] tab= task.GetCommonFriends(UserId,User2Id);

            for(int i=0;i<tab.Length;i++){
                if (tab[i] == FrId)
                    return true;
                }
                return false;
        }


        public string AskToLoginOfFriend(int Fr)
        {
           return task.GetLoginOfFriend(Fr);
            
        }

        public void DelFriend(int DelFr)
        {
            task.DeleteFriend(int.Parse(view.SessionID()), DelFr);
        }

        //Invitations
        public int FindNrOfPagesOfSentInv()
        {
            int M = task.GetNumberOfSentInvitations(int.Parse(view.SessionID()));
            int numberOfPages = 0;
            int N = task.InvitationsPerPage;

            if (M % N != 0)
                numberOfPages = M / N + 1;
            else
                numberOfPages = M / N;

            return numberOfPages;

        }

        public int FindNrOfPagesOfReceivedInv()
        {
            int M = task.GetNumberOfReceivedInvitations(int.Parse(view.SessionID()));
            int numberOfPages = 0;
            int N = task.InvitationsPerPage;

            if (M % N != 0)
                numberOfPages = M / N + 1;
            else
                numberOfPages = M / N;

            return numberOfPages;

        }

        public void FindSentInvitationsPerPage(int pageNr)
        {
            DataTable dt = task.GetSentInvitations(int.Parse(view.SessionID()), pageNr);
            view.ShowFriends(dt,2);
        }

        public void FindReceivedInvitationsPerPage(int pageNr)
        {
            DataTable dt = task.GetReceivedInvitations(int.Parse(view.SessionID()), pageNr);
            view.ShowFriends(dt, 1);
        }

        public void SendInvitation(int Fr)
        {
            task.SendInvitationToFriend(int.Parse(view.SessionID()), Fr);
        }

        public void AcceptInvitation(int Fr)
        {
            task.AcceptInvitation(int.Parse(view.SessionID()), Fr);
        }

        public void RefuseInvitation(int Fr)
        {
            task.RefuseInvitation(int.Parse(view.SessionID()), Fr);
        }

		#endregion Public Methods

		#region Private Methods

		/// <summary>
		/// Gets the forum object from view.
		/// </summary>
		/// <returns></returns>
        private friend GetFriendsObjectFromView()
        {
            friend friendTmp = new friend();

            return friendTmp;
        }

		#endregion Private Methods
		}
	}
    

